<template>
  <div class="contioner">
    <div ref="myChart" :style="{ width: '100%', height: '100%' }"></div>
  </div>
</template>
      
  <script lang="ts" setup>
import * as echarts from 'echarts'
import { Message } from '@arco-design/web-vue'
import { ref, reactive, onMounted, watch } from 'vue'
//引入封装post
import { post } from '@/api/http'
import dayjs from 'dayjs'
//getlist初始化接口参数
let query: any = reactive({
  beginDate: dayjs().format('YYYY-MM'), //当月日期
  enginDate: formatDateTime(
    new Date(Number(new Date().getTime()) + 60 * 1000 * 60 * 24 * 90),
    'yyyy-MM'
  ), //当月日期
    planOrderCode: localStorage.getItem('firmId') || '',
})
function formatDateTime(date: any, format: any) {
  const o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
    'H+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds(), // 毫秒
    a: date.getHours() < 12 ? '上午' : '下午', // 上午/下午
    A: date.getHours() < 12 ? 'AM' : 'PM', // AM/PM
  }
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      )
    }
  }
  return format
}
const option = ref({
  title: {
    text: '织厂流失率走势图',
  },
  tooltip: {
    trigger: 'axis',
  },
  // legend: {
  //   left: '92%',
  //   top: '5%',
  //   align: 'left',
  //   data: ['总流失率'],
  // },
  grid: {
    left: '3%',
    right: '8%',
    bottom: '3%',
    containLabel: true,
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月'],
  },
  yAxis: {
    type: 'value',
    axisLabel: {
      formatter: '{value} %',
    },
  },
  series: [
    {
      name: '总流失率',
      type: 'line',
      data: [25, 78, 15, 45, 86, 95, 47, 45],
      label: { show: true, position: 'top', distance: 10, formatter: '{c}%' },
    },
  ],
})

//初始化
onMounted(async () => {
  selectJt()
    // const state = states()
  // state.setOption(option.value)
})
const myChart: any = ref()
const states = () => {
  return echarts.init(myChart.value)
}
function selectJt() {
  post({
    url: 'org/report/factoryAttritionRate',
    data: {
      firmId: query.planOrderCode, //公司ID
      monthDateStart: query.beginDate,
      monthDateEnd: query.enginDate,
    },
  }).then((res: any) => {
    if (res.code == 200) {
      option.value.xAxis.data = res.data.lineMap.monthList
      option.value.series = res.data.lineMap.lineList.map((res: any) => {
        return {
          name: res.firm,
          type: 'line',
          data: res.attritionRate,
        }
      })
      // option.value.legend.data = res.data.lineMap.lineList.map((o: any) => {
      //   return o.firm
      // })
      const state = states()
      state.setOption(option.value)
    } else {
      Message.error(res.message)
    }
  })
}
</script>
  <script lang="ts">
export default defineComponent({
  name: 'AttritionRateChart',
})
</script>
  <style scoped>
.contioner {
  width: 100%;
  height: 100%;
  background: #fff;
  padding: 10px;
}

:deep(.ag-cell) {
  border-right: 1px solid #dde2eb;
}

:deep(.ag-header-cell) {
  border-right: 1px solid #dde2eb;
}

:deep(.ag-cell-value) {
  user-select: initial;
  -moz-user-select: text;
  -ms-user-select: text;
  -webkit-user-select: text;
}
</style>
      